<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function upload (Request $request)
    {
        $disk = Storage::disk ("public");
        $file = $request->file ("file");
        $file_name = time () . "." . $file->getClientOriginalExtension ();
        $result =    $disk->putFileAs ("images",$file, $file_name);
        if ($result){
            return $this->success ($result, "上传成功");
        }else{
            return  $this->error ("图片上传失败");
        }
    }

    protected function success($data, $msg, $code = 0)
    {
        return response()->json ([
            "code" => $code,
            "msg" => $msg,
            "data" => $data,
        ]);
    }

    protected function error($msg, $code = 1)
    {
        return response()->json ([
            "code" => $code,
            "msg" => $msg,
        ]);
    }
}
